package com.lambda;

import com.siyue.bean.Student;
import org.junit.jupiter.api.Test;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.util.UriComponentsBuilder;

import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class groupBy {


    private static List<Student> studentList=new ArrayList<>();

    static {
        studentList.add(new Student("小红",13,2));
        studentList.add(new Student("张三",20,1));
        studentList.add(new Student("王五",9,1));
        studentList.add(new Student("小明",24,1));

    }

    private static List<Map<String,Object>> mapList=new ArrayList<>();
    static {
        Map<String,Object> map1=new HashMap<>();
        map1.put("name","小红");
        map1.put("age",13);
        map1.put("sex",2);

        Map<String,Object> map2=new HashMap<>();
        map2.put("name","王五");
        map2.put("age",16);
        map2.put("sex",1);

        Map<String,Object> map3=new HashMap<>();
        map3.put("name","张山");
        map3.put("age",9);
        map3.put("sex",2);

        Map<String,Object> map4=new HashMap<>();
        map4.put("name","小祖");
        map4.put("age",13);
        map4.put("sex",1);

        mapList.add(map1);
        mapList.add(map2);
        mapList.add(map3);
        mapList.add(map4);
    }


    /** 对象操作
     * 按性别分组
     */
    @Test
    void contextLoads() {
       Map<Integer,List<Student>> studentMap= studentList.stream().collect(Collectors.groupingBy(Student::getSex));
        System.out.println(studentMap);
        Map<Integer,List<String>> studentMap1= studentList.stream().collect(Collectors.groupingBy(Student::getSex)).entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey,
                e->{
                    return e.getValue().stream().map(Student::getName).collect(Collectors.toList());
                }));
        System.out.println(studentMap1);
    }

    /** map操作
     * 按性别分组
     */
    @Test
    void test1() {
        Map<String,List<Map<String,Object>>> mMap1=  mapList.stream().collect(Collectors.groupingBy(m-> String. valueOf(m.get("age"))));
        System.out.println(mMap1);

        Map<String,List<String>> mMap2=  mapList.stream().collect(Collectors.groupingBy(m-> String.valueOf(m.get("age")))).entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey,
                e->{
                    return e.getValue().stream().map(m->m.get("name").toString()).collect(Collectors.toList());
                }));

        //分组后
        System.out.println(mMap2);
    }





    @Test
    void test2() {
        MultiValueMap<String, String> userRequest = new LinkedMultiValueMap<>();
        userRequest.add("user","admin");
        userRequest.add("id","111111");
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("https://www.baidu.com");
        URI uri = builder.queryParams(userRequest).build().encode().toUri();
        System.out.println(uri);
        System.out.println(true+"");
    }

}
